//
//  LQFirstViewController.m
//  LQWeChat
//
//  Created by liquan on 2017/11/27.
//  Copyright © 2017年 liquan. All rights reserved.
//

#import "LQFirstViewController.h"

static NSString * const CellID = @"UICollectionViewCell";

@interface LQFirstViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic)UICollectionView *collectionView;
@property(nonatomic)UIPageControl *pageControl;
@end

@implementation LQFirstViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageControl];
    
    self.collectionView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    
    self.pageControl.sd_layout
    .centerXEqualToView(self.view)
    .bottomEqualToView(self.view).offset(-40)
    .widthIs(100)
    .heightIs(20);

}


-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        
        layout.minimumInteritemSpacing = 0;
        
        layout.minimumLineSpacing = 0;
        
        layout.itemSize = [UIScreen mainScreen].bounds.size;
        
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.pagingEnabled = YES;
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellID];
        
    }
    return _collectionView;
}

-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor cyanColor];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [LQKUITool createLabelWithText:[NSString stringWithFormat:@"这是第%ld页",indexPath.row] andFont:LQFont(20) andTextColor:[UIColor redColor] andBgColor:[UIColor cyanColor] andTextAlign:NSTextAlignmentCenter];
    
    [cell.contentView addSubview:label];
    
    label.sd_layout
    .centerXEqualToView(cell.contentView)
    .centerYEqualToView(cell.contentView);
    
    label.size_sd = CGSizeMake(200, 30);
    
    if (indexPath.row == 2) {
        UIButton *finishButton = [LQKUITool createButtonWithTitle:@"完成" andFont:LQFont(16) andTextColor:[UIColor greenColor] andBgColor:[UIColor orangeColor]];
        
        [[finishButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            BLOCK_EXEC(self.finishFirstLaunchBlock);
        }];
        
        [cell.contentView addSubview:finishButton];
        
        finishButton.sd_layout.centerXEqualToView(cell.contentView)
        .bottomEqualToView(cell.contentView).offset(-150)
        .widthIs(80)
        .heightIs(30);
    }
   
    
    return cell;
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
